package cj.web.admin.controllers;

import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.dao.AreaDAO;
import cj.web.admin.dao.GameDAO;
import cj.web.admin.dao.GradeDAO;
import cj.web.admin.dao.SchoolDAO;
import cj.web.admin.dao.ShowGameOrderDAO;
import cj.web.admin.domain.Game;
import cj.web.admin.domain.GameOrder;
import cj.web.constants.MemuEnum;
import cj.web.constants.PayStatusEnum;
import cj.web.constants.PayWayEnum;
import cj.web.login.controllers.BaseController;

/**
 * 查询
 * @author Administrator
 *
 */
@Path("/showGameOrder")
public class ShowGameOrderController extends BaseController{

	private static Logger logger = Logger.getLogger(ShowGameOrderController.class.getName());
	
	@Autowired
	private ShowGameOrderDAO showGameOrderDao;
	@Autowired
	private GradeDAO gradeDao;
	@Autowired
	private AreaDAO areaDao;
	@Autowired
	private SchoolDAO schoolDao;
	@Autowired
	private GameDAO gameDao;
	
	@Get("showPermitCode")
	public String showPermitCode(Model model,GameOrder gameOrder) {
        model.add("MemuEnum",MemuEnum.showCodegl);
        List<Game> objs = gameDao.queryAll();
		model.add("gameObjs", objs);
		return "/views/showgame/showPermitCodeList.vm";
	}
	
	@Post("getPermitCodeList")
	public String queryPermitCodeList(GameOrder gameOrder) {
		JSONObject jo = new JSONObject();
		
		List<GameOrder> objs = showGameOrderDao.queryShowGameOrder(gameOrder);
		
		int size = showGameOrderDao.queryShowGameOrderSize(gameOrder);
		jo.put("data", objs);
		jo.put("draw", gameOrder.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("showGameOrder")
	public String showGameOrder(Model model,GameOrder gameOrder) {
		List<Map> grades = gradeDao.asGradeEnum();
		model.add("Grade", grades);
		List<Map> areas = areaDao.asAreaEnum();
		model.add("Area", areas);
		List<Map> schools = schoolDao.asSchoolEnum();
		model.add("School", schools);
		model.add("PayStatus", EnumSet.allOf(PayStatusEnum.class));
		//MemuEnum
        model.add("MemuEnum",MemuEnum.showGamegl);
        List<Game> objs = gameDao.queryAll();
		model.add("gameObjs", objs);
		return "/views/showgame/showGameOrderList.vm";
	}
	
	@Post("getGameOrderList")
	public String queryGameOrderList(GameOrder gameOrder) {
		JSONObject jo = new JSONObject();
		
		List<GameOrder> objs = showGameOrderDao.query(gameOrder);
		
		for(GameOrder obj:objs){
			obj.setPayWayName(obj.getPayWay()!=null?PayWayEnum.getName(obj.getPayWay()):"");
			obj.setStatusName(PayStatusEnum.getName(obj.getStatus()));
		}
		
		int size = showGameOrderDao.querysize(gameOrder);
		jo.put("data", objs);
		jo.put("draw", gameOrder.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id) {
		showGameOrderDao.deleteById(id);
		return "r:showGameOrder";
	}
	
	@Get("recoverById")
	public String recoverById(Model model,@Param("id") int id) {
		showGameOrderDao.recoverById(id);
		return "r:showGameOrder";
	}
}